package com.skzh.unflyzone.service.Impl;

import com.skzh.common.core.domain.model.LoginUser;
import com.skzh.common.utils.DateUtils;
import com.skzh.common.utils.SecurityUtils;
import com.skzh.unflyzone.domain.DbxPoints;
import com.skzh.unflyzone.domain.DbxPolygon;
import com.skzh.unflyzone.domain.dto.DbxCreateDTO;
import com.skzh.unflyzone.mapper.DbxUnflyMapper;
import com.skzh.unflyzone.service.IDbxUnflyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author zr
 * @Description 多边形禁飞区接口实现类
 * @Date 2025/3/26 9:55
 */
@Service
public class DbxUnflyServiceImpl implements IDbxUnflyService {

    @Autowired
    private DbxUnflyMapper dbxUnflyMapper;

    /**
     * 新建多边形禁飞区
     */
    @Override
    @Transactional
    public int insertDbxUnfly(DbxCreateDTO createDTO) {
        // 1.插入多边形主体
        DbxPolygon polygon = new DbxPolygon();
        polygon.setDbxLongitude(createDTO.getDbxLongitude());
        polygon.setDbxLatitude(createDTO.getDbxLatitude());

        // 设置创建信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        polygon.setCreateBy(loginUser.getUsername());
        polygon.setCreateTime(DateUtils.getNowDate());
        //获取当前时间并格式化为字符串
        String currentTimeStr = DateUtils.parseDateToStr("yyyyMMddHHmmss", DateUtils.getNowDate());
        polygon.setDbxName("多边形禁飞区" + currentTimeStr);

        // 插入并获取生成的主键ID
        dbxUnflyMapper.insertDbxUnfly(polygon);
        Long dbxId = polygon.getId();

        // 2.插入顶点数据
        if (createDTO.getPoints() != null && !createDTO.getPoints().isEmpty()) {
            List<DbxPoints> pointsList = createDTO.getPoints().stream()
                    .map(dto -> {
                        DbxPoints point = new DbxPoints();
                        point.setDbxId(dbxId);
                        point.setPointLongitude(dto.getPointLongitude());
                        point.setPointLatitude(dto.getPointLatitude());
                        point.setPointOrder(dto.getPointOrder());
                        point.setCreateBy(loginUser.getUsername());
                        point.setCreateTime(DateUtils.getNowDate());
                        return point;
                    }).collect(Collectors.toList());

            dbxUnflyMapper.batchInsertPoints(pointsList);
        }

        return 1;
    }

    /**
     * 查询多边形禁飞区
     */
    @Override
    public List<DbxPolygon> selectDbxList(DbxPolygon dbxPolygon) {
        return dbxUnflyMapper.selectDbxList(dbxPolygon);
    }

    /**
     * 根据ID批量查询对应多边形的顶点信息
     * */
    @Override
    public List<DbxPoints> selectPointByIds(List<Long> dbxIds) {
       return dbxUnflyMapper.selectPointByIds(dbxIds);
    }

    /**
     * 根据ID删除多边形禁飞区
     */
    @Override
    @Transactional
    public int deleteDbxById(Long id) {
        if (id != null){
            //根据id批量删除多边形对应的顶点信息
            dbxUnflyMapper.deletePointsByDbxId(id);
            dbxUnflyMapper.deleteDbxById(id);
        }
            return 1;
    }

}
